package com.example.demo.service.file.impl;

import com.example.demo.entity.exception.EnumException;
import com.example.demo.entity.exception.EnumResult;
import com.example.demo.service.file.FileService;
import com.example.demo.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description:
 * @author: cj
 * @create: 09-03-11 09:36:57
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Value("${upload.save-path}")
    private String savePath;
    @Value("${http-linux.img-path}")
    private String linuxImgPath;
    @Value("${http-windows.img-path}")
    private String windowsImgPath;

    /**
     * 文件上传
     *
     * @param file 文件数据
     * @return 上传的文件信息map
     */
    @Override
    public Map<String, Object> uploadFile(MultipartFile file) {
        Map<String, Object> fileMap = new HashMap<>( 10 );
        try {

            String imgPath;
            String systemName = System.getProperty( "os.name" );
            System.out.println( "Current System Is : " + systemName );
            String filePath;
            if (!StringUtils.isBlank( systemName ) && systemName.toLowerCase().contains( "linux" )) {
                // Linux临时路径
                filePath = "/blog/source/";
                imgPath = linuxImgPath;
            } else {
                // Windows临时路径
                filePath = "D://blog/source/";
                imgPath = windowsImgPath;
            }
            //上传的文件
            String originalFilename = file.getOriginalFilename();

            //获取文件的后缀 不包含 "."
            String fileType = originalFilename.contains( "." ) ? originalFilename.substring( originalFilename.lastIndexOf( "." ) + 1 ).toLowerCase() : null;
            if (StringUtil.isEmpty( fileType )) {
                //空
                throw new EnumException( EnumResult.EX_EXCEL_ERROR_CODE4 );
            }
            //封装路径 D://blog/source/png/111.png
            String s1 = (new SimpleDateFormat( "yyyyMMddHHmmssSSS" )).format( Calendar.getInstance().getTime() ) + (new Random()).nextInt( 9 );
            String s2 = fileType + "/" + s1 + "." + fileType;
            String s = filePath + "/" + s2;
            File file1 = new File( s );
            if (!file1.exists()) {
                file1.mkdirs();
            }
            //imgPath http://47.96.143.193:8090/img/
            file.transferTo( file1 );

            fileMap.put( "path", imgPath + s2 );
            fileMap.put( "info", "imgPath:" + imgPath + "-filePath:" + filePath
                    + "---s:" + s + "---s2:" + s2 );
            if (fileMap.isEmpty()){
                throw new EnumException( EnumResult.EX_CHART_CODE_ERROR );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileMap;
    }

    public static void main(String[] args) {
        Date time = Calendar.getInstance().getTime();
        int i1 = new Random().nextInt( 9 );
        String yyyyMMddHHmmssSSS = (new SimpleDateFormat( "yyyyMMddHHmmssSSS" )).format( time );
        String s1 = yyyyMMddHHmmssSSS + i1 + "";
        String str = "1.s.s(3).jfif";
        int i = str.lastIndexOf( "." );
        System.out.println( i );
        String substring = str.substring( i + 1 );
        String s12 = (new SimpleDateFormat( "yyyyMMddHHmmssSSS" )).format( Calendar.getInstance().getTime() ) + (new Random()).nextInt( 9 );
        String s = substring + "/" + s12 + "." + substring;
        System.out.println( s );
    }
}
